package com.byt.patient.viewmodel;

import com.byt.base.activity.IPageBaseView;
import com.byt.base.model.BasePageModel;
import com.byt.base.viewmodel.BaseViewModel;
import com.byt.patient.model.PatientMainModel;

import java.util.ArrayList;

/**
 * 类名称：PatientMainViewModel
 * 类描述：代理实现
 */
public class PatientMainViewModel extends
        BaseViewModel<PatientMainViewModel.IPatientMainView, PatientMainModel> implements
        BasePageModel.IModelListener<ArrayList<String>> {

    private ArrayList<String> mList = new ArrayList<>();

    public PatientMainViewModel(long memberId) {
        model = new PatientMainModel(memberId);
        model.register(this);
        model.getCachedDataAndLoad();
    }

    @Override
    public void onLoadFinish(BasePageModel model, ArrayList<String> data, boolean isEmpty, boolean isFirstPage, boolean hasNextPage) {
        if (getPageView() != null) {
            if (model instanceof PatientMainModel) {
                if (isFirstPage) {
                    mList.clear();
                }
                if (isEmpty) {
                    if (isFirstPage) {
                        getPageView().showEmpty();
                    } else {
                        getPageView().onLoadMoreEmpty();
                    }
                } else {
                    mList.addAll(data);
                    getPageView().onPatientMainLoaded(mList);
                }
            }
        }
    }

    @Override
    public void onLoadFail(BasePageModel model, String msg, boolean isFirstPage) {
        if (getPageView() != null) {
            if (isFirstPage) {
                getPageView().onRefreshFailure(msg);
            } else {
                getPageView().onLoadMoreFail(msg);
            }
        }
    }

    public void tryToRefresh() {
        model.refresh();
    }

    public void tryToLoadNextPage() {
        model.loadNexPage();
    }

    public interface IPatientMainView extends IPageBaseView {

        void onPatientMainLoaded(ArrayList<String> data);
    }
}